package com.desmart.renjiao.attendance;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.desmart.renjiao.R;
import com.desmart.renjiao.bean.CheckRecord;
import com.desmart.renjiao.bean.WorkDay;
import com.haibin.calendarview.Calendar;
import com.haibin.calendarview.CalendarView;
import com.lemon.library.common.ARoutePath;
import com.lemon.library.common.CommonActivity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Route(path = ARoutePath.workplace.AttendanceRecordActivity)
public class AttendanceRecordActivity extends CommonActivity<AttendanceRecordM,AttendanceRecordP> implements AttendanceRecord.AttendanceRecordV {

    private CalendarView calendarView;
    private RecyclerView recyclerView;
    private List<CheckRecord> list = new ArrayList<>();
    private AttendanceRecordAdapter adapter;

    @Override
    public int bindLayout() {
        return R.layout.activity_attendance_record;
    }

    @Override
    public void initView(Activity activity) {
        setTitleText("考勤");
        calendarView = activity.findViewById(R.id.calendarView);

        int curYear = calendarView.getCurYear();
        int curMonth = calendarView.getCurMonth();
        setTitleText(curYear + "年" + curMonth + "月");

        initCalendarView();

        recyclerView = activity.findViewById(R.id.recyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AttendanceRecordAdapter(list);
        recyclerView.setAdapter(adapter);

    }

    private void initCalendarView() {
        calendarView.setOnMonthChangeListener(new CalendarView.OnMonthChangeListener() {
            @Override
            public void onMonthChange(int year, int month) {
                setTitleText(year + "年" + month + "月");
                mPresenter.checkServiceByMonth(year,month);
            }
        });
        calendarView.setOnCalendarSelectListener(new CalendarView.OnCalendarSelectListener() {
            @Override
            public void onCalendarOutOfRange(Calendar calendar) {

            }

            @Override
            public void onCalendarSelect(Calendar calendar, boolean isClick) {
                int year = calendar.getYear();
                int month = calendar.getMonth();
                int day = calendar.getDay();
                Log.e("Hqq","选择"+ month + "-" + day);
                mPresenter.checkServiceByDay(year,month,day);
            }
        });

    }

    private Calendar getSchemeCalendar(int year, int month, int day, String text) {
        Calendar calendar = new Calendar();
        calendar.setYear(year);
        calendar.setMonth(month);
        calendar.setDay(day);
        calendar.setSchemeColor(Color.WHITE);
        calendar.setScheme(text);
        calendar.addScheme(0xFFa8b015, "rightTop");
        calendar.addScheme(0xFF423cb0, "leftTop");
        calendar.addScheme(0xFF643c8c, "bottom");
        return calendar;
    }

    @Override
    public void initData(Context context) {

    }

    @Override
    public void onSuccessGetMonthErrorList(List<WorkDay> errorDayList, int year, int month) {
        if (errorDayList == null)
            return;


        Map<String, Calendar> map = new HashMap<>();
        for (WorkDay item : errorDayList){
            String errorDay = item.getErrorDay();
            int day = Integer.valueOf(errorDay);
            map.put(getSchemeCalendar(year, month, day, "").toString(),
                    getSchemeCalendar(year, month, day, ""));

        }
        calendarView.setSchemeDate(map);

    }

    @Override
    public void onSuccessGetDayList(List<CheckRecord> recordsList, List<CheckRecord> errorList) {
        list.clear();
        if (recordsList != null &&  recordsList.size() > 0){
            CheckRecord checkRecord = new CheckRecord(CheckRecord.Title_Record);
            list.add(checkRecord);

            for (CheckRecord item : recordsList){
                item.setType(CheckRecord.Record);
                list.add(item);
            }
        }

        if (errorList != null && errorList.size() > 0){

            CheckRecord checkRecord = new CheckRecord(CheckRecord.Title_Error);
            list.add(checkRecord);

            for (CheckRecord item : errorList){
                item.setType(CheckRecord.Error);
                list.add(item);
            }

        }

        adapter.notifyDataSetChanged();

    }


}
